package zisu.algorithm.algorithm.Stack;

import java.util.Stack;

/**
 * @Author: 蔡翔
 * @Date: 2019/12/3 10:21
 * @Version 1.0
 */
public class SolutionTest {
    public boolean isValid(String s){
        Stack<Character> stack = new Stack<>();
        for(int i =0;i<s.length();i++){
            char a = s.charAt(i);
            if(a == '[' || a == '{' || a == '(' ){
                stack.push(a);
            }else {
                if(stack.isEmpty()){
                    return false;
                }
                char topChar = stack.pop();
                if(a == '[' && topChar != ']'){
                    return false;
                }
                if(a == '(' && topChar != ')'){
                    return false;
                }
                if(a == '{' && topChar != '}'){
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }
}
